package com.benny_driving.act.adapter;

import java.util.List;

import android.content.Context;
import android.util.SparseIntArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.benny_driving.R;
import com.benny_driving.entity.ArgeeMent;

public class ArgeeMentAdapter extends BaseAdapter {
	private List<ArgeeMent> list = null;
	private LayoutInflater layoutInflater = null;
	private SparseIntArray sba;

	public ArgeeMentAdapter(List<ArgeeMent> list, Context context) {
		this.list = list;
		layoutInflater = LayoutInflater.from(context);
		setCheck();
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null) {
			convertView = layoutInflater.inflate(R.layout.adapter_argeement,
					parent, false);
			holder = new ViewHolder();
			System.out.println(holder + "343242343243454");
			holder.title = (TextView) convertView
					.findViewById(R.id.argmentAdp_tv_Title);
			holder.con = (TextView) convertView
					.findViewById(R.id.argmentAdp_tv_Con);
			holder.showDown = (LinearLayout) convertView
					.findViewById(R.id.argmentAdp_tv_ShowDown);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		holder.title.setText(list.get(position).getTitle());
		holder.con.setText(list.get(position).getCon());
		holder.con.setVisibility(sba.get(position));
		holder.showDown.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				if (sba.get(position) == View.VISIBLE) {
					setCheck();
					sba.put(position, View.GONE);
				} else {
					setCheck();
					sba.put(position, View.VISIBLE);
				}
				notifyDataSetInvalidated();
			}
		});
		return convertView;
	}

	class ViewHolder {
		TextView title, con;
		LinearLayout showDown;
	}

	private void setCheck() {
		sba = new SparseIntArray();
		for (int i = 0; i < list.size(); i++) {
			sba.put(i, View.GONE);
		}
	}
}
